package com.bwie.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCe4o4Iy7z8hl5jC597zu3QdoGiqzVNjvk90yx+QRpTtZOHoZCcdfrnY4djxlu+ysFDW0Cxk2yE/WQ+bpUWe/8Sm+uUu/ASjC9Ly2G0xcdao7j0RREdmaBEzNjTqKdXytonMPbx9LGL0s8/V7jJTeDzqwR3VWyKusJyu+kNxxOwEzftdjd43ZIk/PrVdkitx1wxrxZSZ2DU+/q9U1NcXd3C6grA8xGDnEhg+Tp8TVWvP/nCWUzFjNprrQmxUi1qdyMoX8e4uhs3eQAUfvN6j7b+zZnLC14/QWU2NArA812uew3dVkge4JiSvShvnd/flX25AitH4eb6O1+y5zfbXsdBAgMBAAECggEAcuXA8ulq7G67GQMRvPXCkorwalNHQ6QN58Crn02zHS/k1SNoMU08OVKHWQbb19hF9FCsMv46bt3TPTa4x765kZqBvVg/XxpTJYuZR6Hy7UASg0ri+rXa6YX9kuqWwW0nX0oQep6KOmQjjVoBfkm8UFmu7CExUg2GinQLL/Rz71KCx6WX97/OKNieX+DYXdyO7XpDPJq2k8+ZKARaso90z2n1KZ+dTDmEklPwjCsMkacXHsxnL9pFuI3DzkRibpJe3Gc3tRz7xQz/8rLogfgzrSJVhZYvAOLWseOB8z8L2pvx0JMCrImdOy29n4VGKRnWEjWrVkcMxifxput605CUAQKBgQDO/6Jw1WdgVuG/Zc2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wHDV40hlEkDop04n/SLGpKVtkTFxtkLXO4dL5s0RTlAUxy47mQEWHOxpBbYDpoOKIupPmSJsm3RgtYtxvBkc74mlj9FQWR2lfqYoUN17c3lhy+bg5C8td1zwNzbX48MrYkMtVGmHf63oTVWcimV/DRd4hZvhZLl3wLwdodVjjQzmv6g+ycq0QIDAQAB";

    //生成支付订单接口
    public String pay(String orderId,String price) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000141654311");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        request.setReturnUrl("http://localhost:9023/success.html");
        request.setNotifyUrl("http://songxuting.free.idcfengye.com/order/result");

        model.setOutTradeNo(orderId);
        model.setTotalAmount(price);
        model.setSubject("商品购买");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        return response.getBody();
    }
    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000141654311");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
